import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src')
    }
  },
  build: {
    // 打包优化配置
    rollupOptions: {
      output: {
        // 拆分代码块
        manualChunks: {
          'element-plus': ['element-plus', '@element-plus/icons-vue'],
          'vendor': ['vue', 'vue-router'],
          'xlsx': ['xlsx'],
          'utils': ['lodash-es']
        },
        // CSS 代码分割
        assetFileNames: assetInfo => {
          const info = assetInfo.name.split('.')
          const extType = info[info.length - 1]
          if (
            /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/i.test(assetInfo.name)
          ) {
            return `assets/media/[name]-[hash][extname]`
          } else if (
            /\.(png|jpe?g|gif|svg)(\?.*)?$/i.test(assetInfo.name)
          ) {
            return `assets/img/[name]-[hash][extname]`
          } else if (extType === 'css') {
            return `assets/css/[name]-[hash][extname]`
          }
          return `assets/[name]-[hash][extname]`
        }
      }
    },
    // 提高警告阈值，减少大文件警告
    chunkSizeWarningLimit: 1000
  }
}) 